import config from "../../config.js";
import downloadFun from "./download.js";
import chalk from "chalk";
import { buildDownload } from "../../utils/buildDownload.js";
import { removeFilePath } from "../../utils/removeFilePath.js";
import { buildOpts } from "../../utils/buildOpts.js";

const template = async (project, args) => {
	try {
		// 命令行的执行逻辑代码
		let tempFile = `./${project}`;
		const answer = await buildDownload(tempFile, [
			{
				// 数组中每一个对象表示一个问题
				type: "list", // 问题类型，list表示选择列表
				name: "template", // 用于接收答案的键值
				choices: config.template, // 选项
				message: "请选择你所使用的框架", // 问题
			},
		]);
		if (answer.hasOwnProperty("cover")) {
			if (answer.cover) {
				await removeFilePath(tempFile);
			} else {
				tempFile += `-${new Date().getTime()}`;
			}
		}
		let url = config.templateUrls[answer.template] + (buildOpts.checkKey('Fromack') ? `#${buildOpts.get('Fromack')}` : '')
		// 下载代码模板
		downloadFun(url, tempFile).then(
			(res) => {
				console.log(
					chalk.green.bold("Done!"),
					chalk.yellow("you run:")
				);
				console.log(chalk.blue.bold("cd ") + chalk.yellow(tempFile));
				console.log(chalk.blue.bold("pnpm install"));
				console.log(chalk.blue.bold("pnpm run dev:mp-weixin "));
			}
		);
	} catch (err) {
		console.log(err);
	}
};

export default template;
